package com.example.administrator.q1;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/1.
 */

public class FragmentOne extends Fragment implements MyPersonAdapter.InnerItemOnclickListener {


    ListView listView;
    List<Person> myList = new ArrayList<Person>();
    // int [] arr = new int[]{1, 2, 3, 4};
    Person[] personList = new Person[15];

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fragment_one, null);

        listView = (ListView) view.findViewById(R.id.listView_Total);
        // listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        //     @Override
        //     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //         Toast.makeText(getActivity(), "这是" + i , Toast.LENGTH_SHORT).show();
        //     }
        // });

        // 初始化信息

        // String name, String sex, String align, int HP, int Pw, int AC, int Xp
        for (int i = 0; i < 10; i++) {
            personList[i] = new Person("Sir, " + i + 1, i % 2 == 1 ? "Female" : "Male", i % 2 == 0 ? "chaotic" : "Lawful", "p" + (i + 1) + "", i + 6, i + 5, i + 3, 0);
            // Toast.makeText(list_view_Activity.this, personList[i].getImg(), Toast.LENGTH_SHORT).show();
        }

        for (int i = 0; i < 10; i++) {
            myList.add(personList[i]);
        }

        MyPersonAdapter myPersonAdapter = new MyPersonAdapter(getActivity(), myList);
        myPersonAdapter.setOnInnerItemOnclickListener(this);
        // myPersonAdapter.setOnInnerItemOnclickListener(new MyPersonAdapter.InnerItemOnclickListener() {
        //     @Override
        //     public void itemClick(View view) {
        //         int position;
        //         position = (Integer) view.getTag();
        //         switch (view.getId()) {
        //             // TODO: more case.
        //             case R.id.Img_Icon:
        //                 Toast.makeText(getActivity(), "这是" + myList.get(position).getName() + "", Toast.LENGTH_SHORT).show();
        //                 break;
        //             case R.id.AC:
        //                 Toast.makeText(getActivity(), "这是" + myList.get(position).getAC() + "", Toast.LENGTH_SHORT).show();
        //                 break;
        //             case R.id.align:
        //                 Toast.makeText(getActivity(), "这是" + myList.get(position).getAlign() + "", Toast.LENGTH_SHORT).show();
        //                 break;
        //             case R.id.HP:
        //                 Toast.makeText(getActivity(), "这是" + myList.get(position).getHP() + "", Toast.LENGTH_SHORT).show();
        //                 break;
        //             default:
        //                 Toast.makeText(getActivity(), "ButtonMe", Toast.LENGTH_SHORT).show();
        //                 break;
        //         }
        //     }
        // });
        listView.setAdapter(myPersonAdapter);

        return view;
    }

    // public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    //     Toast.makeText(getActivity(), "整体Item", Toast.LENGTH_SHORT).show();
    // }

    @Override
    public void itemClick(View view) {
        int position;
        position = (Integer) view.getTag();
        switch (view.getId()) {
            // TODO: more case.
            case R.id.Img_Icon:
                Toast.makeText(getActivity(), "这是" + myList.get(position).getName() + "", Toast.LENGTH_SHORT).show();
                break;
            case R.id.AC:
                Toast.makeText(getActivity(), "这是" + myList.get(position).getAC() + "", Toast.LENGTH_SHORT).show();
                break;
            case R.id.align:
                Toast.makeText(getActivity(), "这是" + myList.get(position).getAlign() + "", Toast.LENGTH_SHORT).show();
                break;
            case R.id.HP:
                Toast.makeText(getActivity(), "这是" + myList.get(position).getHP() + "", Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(getActivity(), "ButtonMe", Toast.LENGTH_SHORT).show();
                break;
        }
    }

}
